package edu.whut.controller;


import edu.whut.entity.Approval;
import edu.whut.entity.CommonResult;
import edu.whut.service.ApprovalService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RequestMapping("/approval")
@RestController
public class ApprovalController {

    @Resource
    ApprovalService approvalService;

    @RequestMapping("/like")
    public CommonResult<Approval> giveLike(@RequestBody Approval approval){
        if(approvalService.addApproval(approval)){
            return new CommonResult<>(200,"success",null);
        }
        return new CommonResult<>(400,"error",null);
    }

    @RequestMapping("/withdraw")
    public CommonResult<Approval> withdrawLike(@RequestBody Approval approval){
        if(approvalService.deleteApproval(approval)){
            return new CommonResult<>(200,"success",null);
        }
        return new CommonResult<>(400,"error",null);
    }

    @RequestMapping("/getLikeNum")
    public CommonResult<Integer> getLikeNumber(@RequestParam("creationId") Long creationId){
        if(approvalService.getApprovalNumber(creationId)!=null){
            return new CommonResult<>(200,"success",approvalService.getApprovalNumber(creationId));
        }
        return new CommonResult<>(400,"error",null);
    }
}
